/**
 *
 */
package com.naiterui.ehp.bs.doctor.controller;

import com.naiterui.ehp.bp.constants.SwaggerTag;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.support.exception.PublicService;
import com.naiterui.ehp.bp.support.exception.ResponseVO;
import com.naiterui.ehp.bs.doctor.exception.ExceptionCodes;
import com.naiterui.ehp.bs.doctor.service.ICityService;
import com.naiterui.ehp.bs.doctor.vo.CityVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.constraints.NotNull;
import java.util.List;

@RequestMapping("ad/city")
@Validated
@Controller
@Api(tags = {SwaggerTag.DOCTOR + SwaggerTag.BASIC_INFO})
public class AdCityController {

    private static final Logger LOGGER = LoggerFactory.getLogger(AdCityController.class);

    @Autowired
    private ICityService cityService;

    /**
     * <根据城市父级ID查询城市列表>
     * @param pid
     * @return String <返回值描述>
     * @Throws 异常信息
     * @History 2015-6-21 下午5:29:52 by guoyongxiang
     */
    @ApiOperation(value = "根据城市父级ID查询城市列表", notes = "获取城市列表", httpMethod = "POST")
    @ApiImplicitParams({ @ApiImplicitParam(name = "pid", value = "城市父级ID", required = true, dataType = "int", paramType = "query") })
    @RequestMapping("list")
    @ResponseBody
    public ResponseVO<List<CityVO>> getCityList(@NotNull(message = "pid不能为空") Integer pid) throws BusinessException {
        List<CityVO> vo = null;
        try {
            vo = this.cityService.getCityList(pid);
        } catch (Exception e) {
            LOGGER.error("获取城市列表异常,pid={}", pid);
            throw new BusinessException(ExceptionCodes.FAILED);
        }
        return PublicService.returnResponseVO(vo);
    }

    /**
     * 全部城市信息
     *
     * @return
     * @throws BusinessException
     */
    @ApiOperation(value = "城市列表", notes = "获取城市列表", httpMethod = "GET")
    @GetMapping("all")
    @ResponseBody
    public ResponseVO<List<CityVO>> getCityAll() throws BusinessException {
        List<CityVO> vo = null;
        try {
            vo = this.cityService.getCityAll();
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.error("获取城市信息异常", e);
            throw new BusinessException(ExceptionCodes.FAILED);
        }
        return PublicService.returnResponseVO(vo);
    }
}
